const swig = require('swig');
swig.Swig({ varControls: ['%{{', '}}%'], tagControls: ['%{%', '%}%'], cmtControls: ['%{#', '#}%'] });
const fs = require("ma-fs");


var createPages = function (page) {
  const { root_dir, page_dir, pages, widget } = page
  let pagePath = root_dir
  let widgetPath = root_dir + 'widget'
  pages.forEach(_page => createPage(_page, pagePath))
  widget.forEach(_widget => createwidget(_widget, widgetPath))
}


// var page_file = function(tpl, page, tplc = {}) {
//   let { path, name, title, root_dir } = page
//     ".html,.less,.js".split(',').forEach(suffix => {
//       if (suffix == '.html') {
//         let pathFile = `${root_dir}page${path}/${name}`
//         fs.touch(pathFile + suffix, swig.renderFile(`temp/${tpl}.html`,tplc))
//         console.log(pathFile + suffix, "创建成功");
//       } else {
//         let pathFile = `${root_dir}${suffix.slice(1)}${path}/${name}`
//         fs.touch(pathFile + suffix)
//         console.log(pathFile + suffix, "创建成功");
//       }

//     })
// }

var createPage = function (page, pagePath) {
  const { path, title, cover } = page
  let name = path;
  // page_file('page', { path, name, title, root_dir },{title,name})
  // let pathFile = `${pagePath}`
  html_file('page', pagePath, { title, name }, cover)
}
var html_file = function (tpl, pathFile, { title, name }, cover) {
  ".html,.less,.js".split(',').forEach(suffix => {
    if (suffix == '.html') {
      fs.touch(`${pathFile}/${name}${suffix}`, swig.renderFile('temp/' + tpl + '.html', { title, name }), cover)
    } else {
      fs.touch(`${pathFile}/${suffix.split('.')[1]}/${name}${suffix}`)
    }
  })
  console.log(pathFile, "创建成功");
}

var createwidget = function (widget, widgetPath) {
  const { path, name } = widget
  let pathFile = `${widgetPath}${path}/${name}`
  html_file('widget', pathFile, { name })
}

module.exports = {
  createPages
};